﻿Namespace ChatBox

    Module mChatBox

        Dim MidLines As Integer

        Dim WithEvents ChatBox As GUI.MirWindow

        Dim WithEvents txtChatInput As GUI.MirTextBox

        Dim WithEvents btnShrink As GUI.MirButton
        Dim WithEvents btnOpen As GUI.MirButton
        Dim WithEvents btnClose As GUI.MirButton
        Public WithEvents btnResize As GUI.MirButton

        Dim WithEvents btnFilterShout As GUI.MirButton
        Dim WithEvents btnFilterWhisper As GUI.MirButton
        Dim WithEvents btnFilterGroup As GUI.MirButton
        Dim WithEvents btnFilterGuild As GUI.MirButton
        Dim WithEvents btnFilterSystem As GUI.MirButton

        Dim WithEvents ChatScrollBar As GUI.MirScrollBar

        Private ChatFilterBools(4) As Boolean

        Private Enum ChatFilter
            Shout = 0
            Whisper = 1
            Group = 2
            Guild = 3
            System = 4
        End Enum

        Dim mShown As Boolean
        Public mMode As Byte '1 = bar 2 = box 0 = hidden

        Public Structure ChatLine
            Dim Type As Protocol.ChatMsgType
            Dim Name As String
            Dim Msg As String
            Dim Valid As Boolean
            Dim Expires As DateTime
        End Structure

        Dim FirstChatLine As Integer
        Private Const MAX_CHAT_LINES As Integer = 128
        Dim ChatLines(MAX_CHAT_LINES) As ChatLine

        Dim DisplayLines As Integer
        Private Const DisplayLines_OnScreen As Integer = 5

        Dim ScrollPos As Integer

        Public Sub Init()

            ChatBox = New GUI.MirWindow("ChatBox", 0, 0, 360, 26)

            txtChatInput = New GUI.MirTextBox(ChatBox, "txtChatInput", 20, 4, 0, EvoLib.Protocol.MAX_CHATMSG_LEN, DirectX.ProFont9, , , , , EvoLib.Protocol.VALID_CHAT_CHARS)
            txtChatInput.SetFixedWidth(313)
            txtChatInput.SetNormColors(Color.FromArgb(128, 0, 0, 0), Color.FromArgb(128, 0, 0, 0), Color.FromArgb(128, 255, 255, 255))
            txtChatInput.SetFocusedColors(Color.White, Color.White, Color.FromArgb(255, 8, 8, 8))

            btnFilterShout = New GUI.MirButton(ChatBox, "btnFilterShout", 9, 4, 0.0F, ImgLibs.GameInter, 3511, 3512, 3512, "Filter Shout", )
            btnFilterWhisper = New GUI.MirButton(ChatBox, "btnFilterWhisper", 48, 4, 0.0F, ImgLibs.GameInter, 3516, 3517, 3517, "Filter Whispers", )
            btnFilterGroup = New GUI.MirButton(ChatBox, "btnFilterGroup", 87, 4, 0.0F, ImgLibs.GameInter, 3521, 3522, 3522, "Filter Group", )
            btnFilterGuild = New GUI.MirButton(ChatBox, "btnFilterGuild", 126, 4, 0.0F, ImgLibs.GameInter, 3526, 3527, 3527, "Filter Guild", )
            btnFilterSystem = New GUI.MirButton(ChatBox, "btnFilterSystem", 165, 4, 0.0F, ImgLibs.GameInter, 3531, 3532, 3532, "Filter System", )

            btnShrink = New GUI.MirButton(ChatBox, "btnShrink", 344, 4, 0.0F, ImgLibs.GameInter, 3556, 3557, 3557, "Shrink Chat", )
            btnOpen = New GUI.MirButton(ChatBox, "btnOpen", 344, 4, 0.0F, ImgLibs.GameInter, 3551, 3552, 3552, "Open Full Chat", )

            '204,4
            btnClose = New GUI.MirButton(ChatBox, "btnClose", 204, 4, 0.0F, ImgLibs.GameInter, 3546, 3547, 3547, "Close Chat", )

            btnResize = New GUI.MirButton(ChatBox, "btnResize", 344, (DisplayLines * (Libs.GameInter.Image(3503).Header.Height - 1)) - 2, 0.0F, ImgLibs.GameInter, 3541, 3542, 3542, "Resize Chat", )

            ChatScrollBar = New GUI.MirScrollBar(ChatBox, "ChatScrollBar", 341, 23, 18, (DisplayLines * (Libs.GameInter.Image(3503).Header.Height - 1)) - 11, MAX_CHAT_LINES - DisplayLines, EvoLib.Protocol.ImgLibs.GameInter, 3561)

            btnShrink.Visible = False
            btnOpen.Visible = False
            btnClose.Visible = False
            btnFilterShout.Visible = False
            btnFilterWhisper.Visible = False
            btnFilterGroup.Visible = False
            btnFilterGuild.Visible = False
            btnFilterSystem.Visible = False
            ChatScrollBar.visible = False
            btnResize.visible = False

            MidLines = 5
            ToggleSize()

            mMode = 0
            SetModeSmall()

        End Sub

        Public Sub ToggleSize()

            MidLines -= 1
            If MidLines = 0 Then MidLines = 4

            DisplayLines = MidLines + 3

            btnShrink.SetPos(344, 29 + ((Libs.GameInter.Image(3503).Header.Height - 1) * (DisplayLines)))
            txtChatInput.SetPos(20, 29 + ((Libs.GameInter.Image(3503).Header.Height - 1) * (DisplayLines)))
            ChatScrollBar.SetSize(18, (DisplayLines * (Libs.GameInter.Image(3503).Header.Height - 1)) - 11)
            btnResize.SetPos(344, (DisplayLines * (Libs.GameInter.Image(3503).Header.Height - 1)) + 13)

            ChatBox.SetSize(Libs.GameInter.Image(3502).Header.Width, Libs.GameInter.Image(3502).Header.Height + ((Libs.GameInter.Image(3503).Header.Height - 1) * MidLines) + Libs.GameInter.Image(3504).Header.Height)
            ChatBox.SetPosition(163, InterfaceBar.MainInterfaceBarWin.mPosY - (Libs.GameInter.Image(3502).Header.Height + ((Libs.GameInter.Image(3503).Header.Height - 1) * MidLines) + Libs.GameInter.Image(3504).Header.Height))
            ChatScrollBar.Maximum = MAX_CHAT_LINES - DisplayLines

        End Sub

        Public Sub AddChatLine(ByVal line As ChatLine)

            line.Valid = True
            line.Expires = DateTime.Now.AddSeconds(45)

            FirstChatLine += 1
            If FirstChatLine > MAX_CHAT_LINES Then FirstChatLine = 0

            ChatLines(FirstChatLine) = line

            ChatScrollBar.SetValue(MAX_CHAT_LINES - DisplayLines)

        End Sub

        Public Sub Shutdown()

            mShown = False
            ChatBox.Destory()

        End Sub

        Public Sub ChatIn(msg As CharChatMsg, tc As Long)

            Select Case msg.BYTE_TYPE

                Case ChatMsgType.Normal
                    If msg.STR_NAME = Player.PlayerName Then
                        Player.ChatMessage(msg, tc)
                    Else
                        Characters.UpdateCharacterChat(msg, tc)
                    End If

            End Select

            Dim line As ChatLine
            line.Type = msg.BYTE_TYPE
            line.Msg = msg.msg.STR_MSG
            If line.Type = EvoLib.Protocol.mProtocol.ChatMsgType.SentWhisper Then
                line.Name = msg.STR_TONAME
            Else
                line.Name = msg.STR_NAME
            End If

            AddChatLine(line)

        End Sub

        Public Sub SetModeSmall()

            MakeVisible()
            If mMode = 1 Then Exit Sub

            ChatBox.SetSize(360, 26)
            txtChatInput.SetPos(20, 4)
            ChatBox.SetPosition(163, 686)
            btnOpen.Visible = True
            btnShrink.Visible = False
            btnClose.Visible = False
            btnFilterShout.Visible = False
            btnFilterWhisper.Visible = False
            btnFilterGroup.Visible = False
            btnFilterGuild.Visible = False
            btnFilterSystem.Visible = False
            ChatScrollBar.Visible = False
            btnResize.Visible = False


            mMode = 1

        End Sub

        Public Sub SetModeLarge()

            MakeVisible()
            If mMode = 2 Then Exit Sub

            ChatBox.SetSize(Libs.GameInter.Image(3502).Header.Width, Libs.GameInter.Image(3502).Header.Height + ((Libs.GameInter.Image(3503).Header.Height - 1) * MidLines) + Libs.GameInter.Image(3504).Header.Height)
            ChatBox.SetPosition(163, InterfaceBar.MainInterfaceBarWin.mPosY - (Libs.GameInter.Image(3502).Header.Height + ((Libs.GameInter.Image(3503).Header.Height - 1) * MidLines) + Libs.GameInter.Image(3504).Header.Height))
            txtChatInput.SetPos(20, 29 + ((Libs.GameInter.Image(3503).Header.Height - 1) * (DisplayLines)))
            btnShrink.SetPos(344, 29 + ((Libs.GameInter.Image(3503).Header.Height - 1) * (DisplayLines)))

            btnOpen.Visible = False
            btnShrink.Visible = True
            btnClose.Visible = True
            btnFilterShout.Visible = True
            btnFilterWhisper.Visible = True
            btnFilterGroup.Visible = True
            btnFilterGuild.Visible = True
            btnFilterSystem.Visible = True
            ChatScrollBar.Visible = True
            btnResize.Visible = True

            'btnClose.setpos()

            mMode = 2

        End Sub

        Private Sub MakeVisible()
            If Not mShown Then
                Windows.AddWindow(ChatBox, Windows.WindowType.Normal)
                mShown = True
                Windows.FocusInterface(txtChatInput)
            End If
        End Sub

        Private Sub ChatBox_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles ChatBox.DrawWindow

            If mode = Globals.DrawingMode.Text Then

                'DirectX.ProFont9.DrawBorderTextEx(ChatBox.mPosX & ":" & ChatBox.mPosY, x, y, Color.White, Color.Black, z)

            End If

            If mMode = 1 Then 'Slim chat-input box

                If mode = Globals.DrawingMode.Normal Then

                    Libs.GameInter.Image(3501).Draw(x, y, z)

                End If

            ElseIf mMode = 2 Then 'Full chat box

                If mode = Globals.DrawingMode.Normal Then

                    With Libs.GameInter
                        .Image(3502).Draw(x, y, z)
                        For i As Integer = 0 To MidLines - 1
                            .Image(3503).Draw(x, y + .Image(3502).Header.Height + ((.Image(3503).Header.Height - 1) * i), z)
                        Next
                        .Image(3504).Draw(x, y + .Image(3502).Header.Height + ((.Image(3503).Header.Height - 1) * MidLines), z)
                    End With

                ElseIf mode = Globals.DrawingMode.Text Or mode = Globals.DrawingMode.Line Then

                    y = y + 24 + ((Libs.GameInter.Image(3503).Header.Height - 1) * (DisplayLines - 1))
                    x = x + 19



                    Dim i As Integer
                    Dim startat As Integer = FirstChatLine - ScrollPos
                    Dim totaltodraw As Integer = DisplayLines
                    If startat < 0 Then startat += MAX_CHAT_LINES
                    'Dim endat As Integer = startat - (DisplayLines - 2)
                    'If endat < 0 Then
                    'endat += MAX_CHAT_LINES
                    'End If
                    'For i = startat To 0 Step -1

                    'If Not IsFiltered(ChatLines(i).Type) Then
                    '    DrawChatLine(mode, x, y, z, i)
                    '    y -= (Libs.GameInter.Image(3503).Header.Height - 1)
                    'End If

                    ''Next

                    'If i = 0 Then
                    '    If endat > 0 Then
                    '        For i = MAX_CHAT_LINES - 1 To endat Step -1
                    '            DrawChatLine(mode, x, y, z, i)
                    '            y -= (Libs.GameInter.Image(3503).Header.Height - 1)
                    '        Next
                    '    End If
                    'End If
                    'Else
                    '    For i = startat To endat Step -1
                    '        DrawChatLine(mode, x, y, z, i)
                    '        y -= (Libs.GameInter.Image(3503).Header.Height - 1)
                    '    Next
                    'End If

                    Dim drawn As Integer, lines As Integer
                    i = startat

                    Do

                        If Not ChatLines(i).Valid Then Exit Do



                        If Not IsFiltered(ChatLines(i).Type) Then
                            lines = DrawChatLine(mode, x, y, z, i, totaltodraw - drawn)
                            drawn += lines
                            y -= ((Libs.GameInter.Image(3503).Header.Height - 2) * lines)
                        End If

                        If drawn = totaltodraw Then Exit Do

                        i -= 1
                        If i = -1 Then i = MAX_CHAT_LINES
                        If i = startat Then Exit Do

                    Loop


                End If

            End If

        End Sub

        Private Function IsFiltered(T As EvoLib.Protocol.ChatMsgType) As Boolean

            Select Case T

                Case ChatMsgType.Normal
                    Return False

                Case ChatMsgType.GlobalShout, ChatMsgType.LocalShout
                    Return ChatFilterBools(ChatFilter.Shout)

                Case ChatMsgType.Admin, ChatMsgType.Server, ChatMsgType.System
                    Return ChatFilterBools(ChatFilter.System)

                Case ChatMsgType.SentWhisper, ChatMsgType.Whisper
                    Return ChatFilterBools(ChatFilter.Whisper)

                    'TODO: Enable Group/Guild ChatMsgType Filter

                    'Case ChatMsgType.Group
                    '    Return ChatFilterBools(ChatFilter.Group)

                    'Case ChatMsgType.Guild
                    '    Return ChatFilterBools(ChatFilter.Guild)
                    '    
                Case Else
                    Return False


            End Select

        End Function

        Public Sub DrawChatText(mode As Globals.DrawingMode)

            If mMode = 2 Then Exit Sub

            Dim startat As Integer = FirstChatLine
            Dim totaltodraw As Integer = DisplayLines_OnScreen
            If startat < 0 Then startat += MAX_CHAT_LINES

            Dim x As Integer, y As Integer

            y = InterfaceBar.MainInterfaceBarWin.mPosY - 50
            x = InterfaceBar.MainInterfaceBarWin.mPosX + 10

            Dim drawn As Integer, lines As Integer
            Dim i As Integer = startat

            Do

                If Not ChatLines(i).Valid Then Exit Do



                If Not IsFiltered(ChatLines(i).Type) And ( _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.Normal Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.LocalShout Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.GlobalShout Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.Whisper Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.Admin Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.SentWhisper) Then
                    lines = DrawChatLine(mode, x, y, Constants.GameTextZ, i, totaltodraw - drawn, 500, False)
                    drawn += lines
                    y -= ((Libs.GameInter.Image(3503).Header.Height - 1) * lines)
                End If

                If drawn = totaltodraw Then Exit Do

                i -= 1
                If i = -1 Then i = MAX_CHAT_LINES
                If i = startat Then Exit Do

            Loop

            drawn = 0
            lines = 0
            i = startat

            Do

                If Not ChatLines(i).Valid Then Exit Do



                If Not IsFiltered(ChatLines(i).Type) And ( _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.System Or _
                    ChatLines(i).Type = EvoLib.Protocol.mProtocol.ChatMsgType.Server) Then
                    lines = DrawChatLine(mode, Constants.gGameResX - 420, y, Constants.GameTextZ, i, totaltodraw - drawn, 400, False)
                    drawn += lines
                    y -= ((Libs.GameInter.Image(3503).Header.Height - 1) * lines)
                End If

                If drawn = totaltodraw Then Exit Do

                i -= 1
                If i = -1 Then i = MAX_CHAT_LINES
                If i = startat Then Exit Do

            Loop

        End Sub

        Private Function DrawChatLine(mode As Globals.DrawingMode, x As Integer, y As Integer, z As Single, idx As Integer, maxlines As Integer, Optional linewidth As Integer = 322, Optional drawbg As Boolean = True)

            Dim linecount As Integer, drawn As Integer, skipped As Integer

            With ChatLines(idx)
                If Not .Valid Then Return 0
                If .Expires < DateTime.Now And Not drawbg Then Return 0

                Dim tmpstr As String = ""
                Dim chatline As String

                Select Case .Type

                    Case Protocol.ChatMsgType.Normal, Protocol.ChatMsgType.Whisper, Protocol.ChatMsgType.GlobalShout, Protocol.ChatMsgType.LocalShout, Protocol.ChatMsgType.Admin
                        tmpstr = .Name & ": " & .Msg

                    Case Protocol.ChatMsgType.SentWhisper
                        tmpstr = "/" & .Name & " " & .Msg

                    Case Protocol.ChatMsgType.Server, Protocol.ChatMsgType.System
                        tmpstr = .Msg


                End Select
                Dim measurestr As String = tmpstr

                Do Until measurestr.Length = 0

                    Dim i As Integer = 1

                    Do Until i = measurestr.Length
                        If DirectX.ProFont9.GetNormalTextWidth(Left(measurestr, i)) > linewidth Then
                            i -= 1
                            Exit Do
                        End If
                        i += 1
                    Loop

                    measurestr = Right(tmpstr, measurestr.Length - i)
                    linecount += 1

                Loop

                If maxlines < linecount Then
                    y -= ((Libs.GameInter.Image(3503).Header.Height - 2) * (linecount - 1 - (linecount - maxlines)))
                Else
                    y -= ((Libs.GameInter.Image(3503).Header.Height - 2) * (linecount - 1))
                End If

                Do

                    Dim i As Integer

                    For i = 1 To tmpstr.Length

                        If DirectX.ProFont9.GetNormalTextWidth(Left(tmpstr, i)) > linewidth Then

                            'i -= 1
                            Exit For

                        End If

                    Next

                    chatline = Left(tmpstr, i)
                    tmpstr = Right(tmpstr, tmpstr.Length - chatline.Length)

                    If linecount <= maxlines Or skipped + maxlines >= linecount Then

                        Select Case .Type

                            Case Protocol.ChatMsgType.Normal
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.Black, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.White, Color.Black, z - 0.001F)
                                    End If
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.White)
                                End If

                            Case Protocol.ChatMsgType.Whisper
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.Blue, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.SkyBlue, Color.Black, z - 0.001F)
                                    End If
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.White)
                                End If

                            Case Protocol.ChatMsgType.SentWhisper
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.Blue, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.SkyBlue, Color.Black, z - 0.001F)
                                    End If
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.White)
                                End If

                            Case Protocol.ChatMsgType.GlobalShout
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.Black, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.Green, Color.Black, z - 0.001F)
                                    End If

                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.LawnGreen)
                                End If

                            Case Protocol.ChatMsgType.LocalShout
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.Black, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.Yellow, Color.Black, z - 0.001F)
                                    End If
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.Gold)
                                End If

                            Case Protocol.ChatMsgType.Admin
                                If mode = Globals.DrawingMode.Text Then
                                    DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.Red, Color.Black, z - 0.001F)
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.Red)
                                End If

                            Case Protocol.ChatMsgType.Server
                                If mode = Globals.DrawingMode.Text Then
                                    If drawbg Then
                                        DirectX.ProFont9.DrawNormalText(chatline, x + 2, y + 2, Color.White, z - 0.001F)
                                    Else
                                        DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.Blue, Color.Black, z - 0.001F)
                                    End If
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.Blue)
                                End If

                            Case Protocol.ChatMsgType.System
                                If mode = Globals.DrawingMode.Text Then
                                    DirectX.ProFont9.DrawBorderText(chatline, x + 2, y + 2, Color.Red, Color.Black, z - 0.001F)
                                ElseIf mode = Globals.DrawingMode.Line Then
                                    If drawbg Then Engine.DrawSolidBox(New Rectangle(x, y, 322, (Libs.GameInter.Image(3503).Header.Height - 1)), z, Color.White)
                                End If

                        End Select

                        y += (Libs.GameInter.Image(3503).Header.Height - 1)

                        drawn += 1
                        If drawn = maxlines Then Exit Do

                    Else
                        skipped += 1
                    End If

                Loop Until tmpstr.Length = 0

            End With

            Return drawn

        End Function

        Private Sub txtChatInput_Enter() Handles txtChatInput.Enter

            If txtChatInput.Text.Length > 0 Then

                Dim pkt As EvoLib.Protocol.Packet
                pkt.Username = Globals.sUsername
                pkt.PacketType = EvoLib.Protocol.GamePacketType.ChatMsg
                pkt.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry

                Dim struct As EvoLib.Protocol.ChatMsg
                struct.STR_MSG = txtChatInput.Text

                ClientSock.SendData(EvoLib.Protocol.MakePacket(pkt, struct, Globals.Crypto))

            End If

            txtChatInput.Text = ""
            If mMode = 1 Then ToggleShowChat()

        End Sub

        Public Sub ToggleShowChat()
            If mShown Then
                Windows.RemoveWindow(ChatBox)
                mShown = False
                txtChatInput.Text = ""
            Else
                Windows.AddWindow(ChatBox, Windows.WindowType.Normal)
                mShown = True
                Windows.FocusInterface(txtChatInput)
            End If
        End Sub

        Private Sub ChatBox_MouseEvent(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs) Handles ChatBox.MouseEvent

            GameWindow.HideTooltip()

        End Sub

        Private Sub btnClose_Click() Handles btnClose.Click
            ToggleShowChat()
        End Sub

        Private Sub btnShrink_Click() Handles btnShrink.Click
            SetModeSmall()
        End Sub

        Private Sub btnOpen_Click() Handles btnOpen.Click
            SetModeLarge()
        End Sub

        Private Sub btnFilterShout_Click() Handles btnFilterShout.Click
            ChatFilterBools(ChatFilter.Shout) = Not ChatFilterBools(ChatFilter.Shout)

            Dim M As New CharChatMsg
            M.BYTE_TYPE = ChatMsgType.System
            If ChatFilterBools(ChatFilter.Shout) Then
                M.msg.STR_MSG = "Filtering out shouts"
            Else
                M.msg.STR_MSG = "Showing shouts"
            End If
            ChatIn(M, DateTime.Now.Ticks)

        End Sub

        Private Sub btnFilterWhisper_Click() Handles btnFilterWhisper.Click
            ChatFilterBools(ChatFilter.Whisper) = Not ChatFilterBools(ChatFilter.Whisper)

            Dim M As New CharChatMsg
            M.BYTE_TYPE = ChatMsgType.System
            If ChatFilterBools(ChatFilter.Whisper) Then
                M.msg.STR_MSG = "Filtering out whispers"
            Else
                M.msg.STR_MSG = "Showing whispers"
            End If
            ChatIn(M, DateTime.Now.Ticks)

        End Sub

        Private Sub btnFilterGroup_Click() Handles btnFilterGroup.Click
            ChatFilterBools(ChatFilter.Group) = Not ChatFilterBools(ChatFilter.Group)

            Dim M As New CharChatMsg
            M.BYTE_TYPE = ChatMsgType.System
            If ChatFilterBools(ChatFilter.Group) Then
                M.msg.STR_MSG = "Filtering out group chat"
            Else
                M.msg.STR_MSG = "Showing group chat"
            End If
            ChatIn(M, DateTime.Now.Ticks)

        End Sub

        Private Sub btnFilterGuild_Click() Handles btnFilterGuild.Click
            ChatFilterBools(ChatFilter.Guild) = Not ChatFilterBools(ChatFilter.Guild)

            Dim M As New CharChatMsg
            M.BYTE_TYPE = ChatMsgType.System
            If ChatFilterBools(ChatFilter.Guild) Then
                M.msg.STR_MSG = "Filtering out guild chat"
            Else
                M.msg.STR_MSG = "Showing guild chat"
            End If
            ChatIn(M, DateTime.Now.Ticks)

        End Sub

        Private Sub btnFilterSystem_Click() Handles btnFilterSystem.Click
            ChatFilterBools(ChatFilter.System) = Not ChatFilterBools(ChatFilter.System)

            Dim M As New CharChatMsg
            M.BYTE_TYPE = ChatMsgType.System
            If ChatFilterBools(ChatFilter.System) Then
                M.msg.STR_MSG = "Filtering out system messages"
            Else
                M.msg.STR_MSG = "Showing system messages"
            End If
            ChatIn(M, DateTime.Now.Ticks)


        End Sub

        Private Sub btnClose_RequestingTooltip(tttxt As String) Handles btnClose.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFilterGroup_RequestingTooltip(tttxt As String) Handles btnFilterGroup.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFilterGuild_RequestingTooltip(tttxt As String) Handles btnFilterGuild.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFilterShout_RequestingTooltip(tttxt As String) Handles btnFilterShout.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFilterSystem_RequestingTooltip(tttxt As String) Handles btnFilterSystem.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFilterWhisper_RequestingTooltip(tttxt As String) Handles btnFilterWhisper.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnOpen_RequestingTooltip(tttxt As String) Handles btnOpen.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnShrink_RequestingTooltip(tttxt As String) Handles btnShrink.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnResize_Click() Handles btnResize.Click
            ToggleSize()
        End Sub

        Private Sub ChatScrollBar_Changed() Handles ChatScrollBar.Changed
            ScrollPos = (MAX_CHAT_LINES - DisplayLines) - (ChatScrollBar.Value + 1)
        End Sub

        Private Sub btnResize_RequestingTooltip(tttxt As String) Handles btnResize.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

    End Module

End Namespace